package serverlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import until.Dofaq.Actionfaq;
import connectDB.CustomerDB;
import connectDB.FaqDB;
import connectDB.IDB;
import connectDB.PacketDB;
import entities.Customerentities;
import entities.Faqentities;
import entities.Packetentities;

/**
 * Servlet implementation class Faqcontroller
 */
@WebServlet("/Customercontroller")
public class Customercontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request,response);
	}

	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("btaction");
		
		
		if(action.equals("delete")){
				this.doDelete(request, response);
		}	
		

		if(action.equals("edit"))   //edit
			{
				this.doEdit(request, response);
			}
			
			  else if(action.equals("add"))
				{
				this.doAdd(request, response);
				}
				
	}
	

	private void doEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException {


		
		try {
			IDB<Customerentities, Boolean, String> acc = new CustomerDB();
			int id = Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("name");
			String cardid = request.getParameter("cardid");
			int packid = Integer.parseInt(request.getParameter("packetid"));
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String services = request.getParameter("services");
			double paymonth = Double.parseDouble(request.getParameter("paymonth"));
			String userid = request.getParameter("userid");	// ====>


			Customerentities fa = new Customerentities(id, name, cardid, packid, phone, email, address, services, paymonth, userid);
			boolean result = acc.update(fa);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/customerlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {


		try {
			IDB<Customerentities, Boolean, String> acc = new CustomerDB();
			int id = 0;
			String name = request.getParameter("name");
			String cardid = request.getParameter("cardid");
			int packid = Integer.parseInt(request.getParameter("packetid"));  // ====>
			String phone = request.getParameter("phone");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String services = request.getParameter("services");
			double paymonth = Double.parseDouble(request.getParameter("paymonth"));
			String userid = request.getParameter("userid");	// ====>
			
			//Packetentities land = new Packetentities(packetid, packetname);
			Customerentities land = new Customerentities(id, name, cardid, packid, phone, email, address, services, paymonth, userid);
			boolean result = acc.add(land);
			String url = null;
			if(result){
				HttpSession session = request.getSession(true);
				url = "/Manager/faqlist.jsp";
			}
			RequestDispatcher rd = request.getRequestDispatcher(url);
			rd.forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {


		try {	
			
			int x = Integer.parseInt(req.getParameter("id"));
			
			IDB<Customerentities,Boolean,String> acc = new CustomerDB();
			
			boolean result = acc.delete1(x);
			String url = "/Manager/customerlist.jsp";
			RequestDispatcher dis = req.getRequestDispatcher(url);
			dis.forward(req, resp);
			
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}

}
